Skip to content

fix(postgrest): throw on multi-row result from maybeSingle when throw-on-error is enabled - #1180

Open
grdsdev wants to merge 2 commits into
mainfrom
fix/postgrest-maybesingle-multi-row
Open

fix(postgrest): throw on multi-row result from maybeSingle when throw-on-error is enabled#1180
grdsdev wants to merge 2 commits into
mainfrom
fix/postgrest-maybesingle-multi-row

Conversation

@grdsdev

@grdsdev grdsdev commented Aug 6, 2026

Copy link
Copy Markdown
Contributor

What

maybeSingle() unconditionally swallowed PGRST116 ("no/too many rows"), so a query that unexpectedly matched more than one row silently returned nil instead of surfacing an error — hiding a real bug where the query should have been scoped to match at most one row.

  • Zero rows: still returns nil (unchanged)
  • More than one row: now throws a PostgrestError, matching the behavior of single()

PostgREST reports both cases with the same PGRST116 code; the row count is only distinguishable via the details message ("Results contain N rows, application/vnd.pgrst.object+json requires 1 row"), so maybeSingle() now inspects that to decide whether to swallow or rethrow.

Fixes #1170

Depends on #1159

This fix relies on PostgrestError.details actually decoding from the server response — which it doesn't today on main (see #1159: the wire key is "details", but the property/synthesized decoding key was detail, so it silently dropped).

The first commit on this branch (fix(helpers): decode PostgREST error details field) mirrors #1159's change so this PR is self-contained and testable independently. Once #1159 merges into main, rebasing this branch will produce a trivial no-op conflict on that one commit (identical change) — drop it and keep the second commit.

Verification

  • swift test --filter "PostgRESTTests|HelpersTests" — 229 tests pass
  • swift package diagnose-api-breaking-changes origin/main — no breaking changes in any module
  • ./scripts/format.sh — no-op

Acceptance criteria

  • Fix implemented matching the intended maybeSingle() behavior
  • Unit tests cover zero-row (unchanged), single-row (unchanged), and multi-row (new, throws)
  • Doc comments updated
  • No breaking changes to existing public API

@grdsdev
grdsdev requested a review from a team as a code owner August 6, 2026 20:08
@coderabbitai

coderabbitai Bot commented Aug 6, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro Plus

Run ID: 45c81a83-e8e8-4c93-8dad-b04d9c941f57

📥 Commits

Reviewing files that changed from the base of the PR and between f894afd and cc7dfd3.

📒 Files selected for processing (2)
  • Sources/PostgREST/PostgrestBuilder.swift
  • Tests/PostgRESTTests/PostgrestBuilderTests.swift
🚧 Files skipped from review as they are similar to previous changes (2)
  • Tests/PostgRESTTests/PostgrestBuilderTests.swift
  • Sources/PostgREST/PostgrestBuilder.swift

📝 Walkthrough

Summary by CodeRabbit

  • Bug Fixes

    • maybeSingle() now returns nil only when no rows match.
    • Multiple matching rows correctly produce a PGRST116 error.
    • Zero-row responses with updated PostgREST error wording are handled correctly.
  • Improvements

    • PostgREST errors now expose additional information through details.
    • Existing detail usage remains supported for compatibility.
    • Error details are handled consistently when decoding and encoding responses.
  • Documentation

    • Clarified maybeSingle() behavior for zero and multiple matching rows.

Walkthrough

PostgrestError now stores error details under details and preserves deprecated detail compatibility APIs. maybeSingle() parses PGRST116 details and returns nil only for zero rows. Multiple-row responses throw PostgrestError. Tests cover serialization, compatibility, zero rows, and multiple rows. Documentation and SDK compliance metadata were updated.

Sequence Diagram(s)

sequenceDiagram
  participant PostgrestBuilder
  participant PostgREST
  participant PostgrestError
  PostgrestBuilder->>PostgREST: Execute maybeSingle request
  PostgREST-->>PostgrestBuilder: Return data or PGRST116 error
  PostgrestBuilder->>PostgrestError: Parse details
  PostgrestError-->>PostgrestBuilder: Identify zero-row or multiple-row result
  PostgrestBuilder-->>PostgrestBuilder: Return nil or rethrow error
Loading

Assessment against linked issues

Objective Addressed Explanation
Distinguish zero-row and multiple-row PGRST116 responses in maybeSingle() [#1170]
Preserve public API compatibility while adopting Swift naming [#1170]
Add tests and update documentation [#1170]

Out-of-scope changes

Code Change Explanation
Add pkey to the dictionary (dictionary.txt:126) This does not relate to maybeSingle() error handling or the PostgrestError API.

Possibly related PRs

  • supabase/supabase-swift#1159: Shares the PostgrestError.details migration, compatibility APIs, tests, dictionary entry, and SDK compliance updates.

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@grdsdev
grdsdev force-pushed the fix/postgrest-maybesingle-multi-row branch from cac1794 to f894afd Compare August 7, 2026 13:01
@coveralls

coveralls commented Aug 7, 2026

Copy link
Copy Markdown

Coverage Report for CI Build 31187909468

Warning

Build has drifted: This PR's base is out of sync with its target branch, so coverage data may include unrelated changes.
Quick fix: rebase this PR. Learn more →

Coverage increased (+0.3%) to 84.231%

Details

  • Coverage increased (+0.3%) from the base build.
  • Patch coverage: 3 uncovered changes across 1 file (14 of 17 lines covered, 82.35%).
  • No coverage regressions found.

Uncovered Changes

File Changed Covered %
Sources/Helpers/SharedModels/PostgrestError.swift 5 2 40.0%
Total (2 files) 17 14 82.35%

Coverage Regressions

No coverage regressions found.


Coverage Stats

Coverage Status
Relevant Lines: 10337
Covered Lines: 8707
Line Coverage: 84.23%
Coverage Strength: 39.44 hits per line

💛 - Coveralls

@github-actions

github-actions Bot commented Aug 7, 2026

Copy link
Copy Markdown
Contributor

⚠️ Capability matrix drift detected

The following capabilities are marked implemented in swift but have no registered symbols to verify:

  • auth.passkey.register_passkey (no symbols list — cannot confirm implementation exists)
  • auth.passkey.sign_in_with_passkey (no symbols list — cannot confirm implementation exists)
  • client.authentication_integration.third_party_auth (no symbols list — cannot confirm implementation exists)
  • client.authentication_integration.cross_client_token_sync (no symbols list — cannot confirm implementation exists)
  • client.authentication_integration.oauth_flow_type (no symbols list — cannot confirm implementation exists)
  • client.authentication_integration.session_url_detection (no symbols list — cannot confirm implementation exists)
  • client.session_management.custom_storage (no symbols list — cannot confirm implementation exists)
  • client.session_management.persist_session (no symbols list — cannot confirm implementation exists)
  • client.request_configuration.global_headers (no symbols list — cannot confirm implementation exists)
  • client.observability.trace_propagation (no symbols list — cannot confirm implementation exists)
  • database.query.select (no symbols list — cannot confirm implementation exists)
  • database.query.schema_selection (no symbols list — cannot confirm implementation exists)
  • database.query.rpc (no symbols list — cannot confirm implementation exists)
  • database.mutate.insert (no symbols list — cannot confirm implementation exists)
  • database.mutate.update (no symbols list — cannot confirm implementation exists)
  • database.mutate.upsert (no symbols list — cannot confirm implementation exists)
  • database.mutate.delete (no symbols list — cannot confirm implementation exists)
  • database.mutate.select_after_mutation (no symbols list — cannot confirm implementation exists)
  • database.using_filters.eq (no symbols list — cannot confirm implementation exists)
  • database.using_filters.neq (no symbols list — cannot confirm implementation exists)
  • database.using_filters.gt (no symbols list — cannot confirm implementation exists)
  • database.using_filters.gte (no symbols list — cannot confirm implementation exists)
  • database.using_filters.lt (no symbols list — cannot confirm implementation exists)
  • database.using_filters.lte (no symbols list — cannot confirm implementation exists)
  • database.using_filters.like (no symbols list — cannot confirm implementation exists)
  • database.using_filters.ilike (no symbols list — cannot confirm implementation exists)
  • database.using_filters.is (no symbols list — cannot confirm implementation exists)
  • database.using_filters.in (no symbols list — cannot confirm implementation exists)
  • database.using_filters.contains (no symbols list — cannot confirm implementation exists)
  • database.using_filters.contained_by (no symbols list — cannot confirm implementation exists)
  • database.using_filters.range_gt (no symbols list — cannot confirm implementation exists)
  • database.using_filters.range_gte (no symbols list — cannot confirm implementation exists)
  • database.using_filters.range_lt (no symbols list — cannot confirm implementation exists)
  • database.using_filters.range_lte (no symbols list — cannot confirm implementation exists)
  • database.using_filters.range_adjacent (no symbols list — cannot confirm implementation exists)
  • database.using_filters.overlaps (no symbols list — cannot confirm implementation exists)
  • database.using_filters.text_search (no symbols list — cannot confirm implementation exists)
  • database.using_filters.match (no symbols list — cannot confirm implementation exists)
  • database.using_filters.not (no symbols list — cannot confirm implementation exists)
  • database.using_filters.or (no symbols list — cannot confirm implementation exists)
  • database.using_filters.raw (no symbols list — cannot confirm implementation exists)
  • database.using_filters.regex (no symbols list — cannot confirm implementation exists)
  • database.using_filters.regex_icase (no symbols list — cannot confirm implementation exists)
  • database.using_filters.is_distinct (no symbols list — cannot confirm implementation exists)
  • database.using_filters.like_all (no symbols list — cannot confirm implementation exists)
  • database.using_filters.like_any (no symbols list — cannot confirm implementation exists)
  • database.using_filters.ilike_all (no symbols list — cannot confirm implementation exists)
  • database.using_filters.ilike_any (no symbols list — cannot confirm implementation exists)
  • database.using_modifiers.order (no symbols list — cannot confirm implementation exists)
  • database.using_modifiers.limit (no symbols list — cannot confirm implementation exists)
  • database.using_modifiers.range (no symbols list — cannot confirm implementation exists)
  • database.using_modifiers.single_row (no symbols list — cannot confirm implementation exists)
  • database.using_modifiers.strip_nulls (no symbols list — cannot confirm implementation exists)
  • database.using_modifiers.format_csv (no symbols list — cannot confirm implementation exists)
  • database.using_modifiers.format_geojson (no symbols list — cannot confirm implementation exists)
  • database.using_modifiers.max_affected_rows (no symbols list — cannot confirm implementation exists)
  • database.using_modifiers.request_cancellation (no symbols list — cannot confirm implementation exists)
  • database.configuration.auto_retry (no symbols list — cannot confirm implementation exists)
  • functions.invocation.invoke (no symbols list — cannot confirm implementation exists)
  • functions.invocation.set_auth_token (no symbols list — cannot confirm implementation exists)
  • functions.invocation.method_override (no symbols list — cannot confirm implementation exists)
  • functions.invocation.streaming_response (no symbols list — cannot confirm implementation exists)
  • functions.invocation.request_cancellation (no symbols list — cannot confirm implementation exists)
  • realtime.client.connect (no symbols list — cannot confirm implementation exists)
  • realtime.client.disconnect (no symbols list — cannot confirm implementation exists)
  • realtime.client.get_channels (no symbols list — cannot confirm implementation exists)
  • realtime.client.remove_channel (no symbols list — cannot confirm implementation exists)
  • realtime.client.remove_all_channels (no symbols list — cannot confirm implementation exists)
  • realtime.client.connection_state (no symbols list — cannot confirm implementation exists)
  • realtime.client.listen_heartbeats (no symbols list — cannot confirm implementation exists)
  • realtime.client.set_auth_token (no symbols list — cannot confirm implementation exists)
  • realtime.client.channel (no symbols list — cannot confirm implementation exists)
  • realtime.channel.subscribe (no symbols list — cannot confirm implementation exists)
  • realtime.channel.unsubscribe (no symbols list — cannot confirm implementation exists)
  • realtime.channel.broadcast (no symbols list — cannot confirm implementation exists)
  • realtime.channel.broadcast_http (no symbols list — cannot confirm implementation exists)
  • realtime.subscriptions.postgres_changes (no symbols list — cannot confirm implementation exists)
  • realtime.subscriptions.subscribe_presence (no symbols list — cannot confirm implementation exists)
  • realtime.subscriptions.private_channel (no symbols list — cannot confirm implementation exists)
  • realtime.subscriptions.broadcast_self (no symbols list — cannot confirm implementation exists)
  • realtime.subscriptions.broadcast_ack (no symbols list — cannot confirm implementation exists)
  • realtime.subscriptions.broadcast_replay (no symbols list — cannot confirm implementation exists)
  • realtime.presence.track (no symbols list — cannot confirm implementation exists)
  • realtime.presence.untrack (no symbols list — cannot confirm implementation exists)
  • realtime.presence.presence_key (no symbols list — cannot confirm implementation exists)
  • realtime.configuration.custom_websocket_transport (no symbols list — cannot confirm implementation exists)
  • realtime.configuration.reconnect_backoff (no symbols list — cannot confirm implementation exists)
  • realtime.configuration.heartbeat_interval (no symbols list — cannot confirm implementation exists)
  • realtime.configuration.access_token_callback (no symbols list — cannot confirm implementation exists)
  • realtime.configuration.deferred_disconnect (no symbols list — cannot confirm implementation exists)
  • realtime.configuration.custom_logger (no symbols list — cannot confirm implementation exists)
  • realtime.configuration.binary_protocol (no symbols list — cannot confirm implementation exists)
  • storage.file_buckets.get_bucket (no symbols list — cannot confirm implementation exists)
  • storage.file_buckets.list_file_buckets (no symbols list — cannot confirm implementation exists)
  • storage.file_buckets.update_bucket (no symbols list — cannot confirm implementation exists)
  • storage.file_buckets.delete_file_bucket (no symbols list — cannot confirm implementation exists)
  • storage.file_buckets.empty_bucket (no symbols list — cannot confirm implementation exists)
  • storage.file_buckets.access_bucket (no symbols list — cannot confirm implementation exists)
  • storage.file_buckets.upload (no symbols list — cannot confirm implementation exists)
  • storage.file_buckets.download (no symbols list — cannot confirm implementation exists)
  • storage.file_buckets.move (no symbols list — cannot confirm implementation exists)
  • storage.file_buckets.copy (no symbols list — cannot confirm implementation exists)
  • storage.file_buckets.remove (no symbols list — cannot confirm implementation exists)
  • storage.file_buckets.create_signed_url (no symbols list — cannot confirm implementation exists)
  • storage.file_buckets.create_signed_urls (no symbols list — cannot confirm implementation exists)
  • storage.file_buckets.create_signed_upload_url (no symbols list — cannot confirm implementation exists)
  • storage.file_buckets.upload_with_signed_url (no symbols list — cannot confirm implementation exists)
  • storage.file_buckets.update_file (no symbols list — cannot confirm implementation exists)
  • storage.file_buckets.file_exists (no symbols list — cannot confirm implementation exists)
  • storage.file_buckets.file_info (no symbols list — cannot confirm implementation exists)
  • storage.file_buckets.copy_cross_bucket (no symbols list — cannot confirm implementation exists)
  • storage.file_buckets.move_cross_bucket (no symbols list — cannot confirm implementation exists)
  • storage.file_buckets.upload_with_metadata (no symbols list — cannot confirm implementation exists)
  • storage.file_buckets.url_cache_nonce (no symbols list — cannot confirm implementation exists)

These may have been renamed, removed, or never registered. Please update the capability matrix.
See: https://github.com/supabase/sdk/blob/main/docs/capability-matrix.md

grdsdev added 2 commits August 7, 2026 11:51
…-on-error is enabled

maybeSingle() swallowed PGRST116 unconditionally, so a query that matched
more than one row silently returned nil instead of surfacing the error --
hiding a real bug where the query should have been scoped to match at most
one row. Zero rows still returns nil; more than one row now throws, same as
single() already does.

PostgREST reports both cases with the same error code, distinguishable only
via the details message ("Results contain N rows, ..."), so maybeSingle now
inspects that to decide whether to swallow or rethrow.

Fixes #1170
maybeSingle()'s zero-vs-multiple-row detection only matched the
"Results contain N rows" wording, but the real PostgREST server (used
in the Linux integration CI job) instead sends "The result contains N
rows", causing maybeSingle() to rethrow on zero rows and fail the
maybeSingleReturnsNilOnZeroRows integration test. Extract the row
count by scanning for the number preceding a "row"/"rows" token
instead of matching a fixed prefix, so both wordings work.
@grdsdev
grdsdev force-pushed the fix/postgrest-maybesingle-multi-row branch from cc7dfd3 to 2f307f5 Compare August 7, 2026 14:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

parity(postgrest): throw on multi-row result from maybeSingle when throw-on-error is enabled [from supabase-js]

2 participants